package com.gopay.bis.web.controller.ebank;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.bank.NuccConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.nucc.ebank.pay.end.NuccEbankEndRes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 网联网关支付后台通知
 * @NuccEbankServerRes.java
 * @author zhoutong
 * @2018-1-30 下午5:07:06 www.gopay.com.cn Inc.All rights reserved.
 */
@Controller
@Scope("prototype")
public class NuccEbankServerRes extends BankResBase {

	private static Log logger = LogFactory.getLog(NuccEbankServerRes.class);

	@RequestMapping(value = "/nuccEbankServerRes")
	protected ModelAndView execute(HttpServletRequest request,
			HttpServletResponse response) {
		logger.error("--------------- nuccEbankServerRes bankback ---------------");
		return super.execute(request, response);
	}

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		NuccEbankEndRes message = new NuccEbankEndRes();
		try {
			String msg = request.getParameter("epccGwMsg");
			logger.info("网联网关支付终态通知报文解码前："+msg);
			//base64解码
			String decodeMsg = new String(Base64.decodeBase64(msg), "UTF-8");
			logger.info("网联网关支付终态通知报文解码后："+decodeMsg);
			message.setSignMessage(decodeMsg);
			// 银行码赋值
			message.setBank(BankConstants.BANK_LOGO_NUCCPAY);
			//银行业务产品01企业网银支付
			message.setBankBusProd(BisBankBusinessProd._01);
			// 银行接口通道信息 18: 网联
			message.setBankIntrChann(BisBankInterfaceChannel._18);
			message.setFront(false);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		OrderInfo orderInfo = new OrderInfo();
		NuccEbankEndRes stream = (NuccEbankEndRes) result.getModel("res");
		String trxStatus = stream.getMsgBody().getBizInf().getTrxStatus();//状态
		// 验签成功入账
		if (NuccConstants.NUCC_TRXSTATUSCD_SUCCESS.equals(trxStatus)
				|| NuccConstants.NUCC_EXCEPTION_SUCCESS.equals(trxStatus)) {
			orderInfo.setOrderId(stream.getOrderId());
			orderInfo.setAmt(stream.getAmount().toString());
			orderInfo.setNotifyNum("1");
			orderInfo.setPayStatus("");
			orderInfo.setRetCode("00");
			orderInfo.setRetMsg("");
		} else {
			orderInfo.setOrderId(stream.getOrderId());
			orderInfo.setAmt(stream.getAmount().toString());
			orderInfo.setNotifyNum("1");
			orderInfo.setPayStatus("01");
			orderInfo.setRetCode("01");
			String sysRtnCd = stream.getMsgBody().getSysRtnInf().getSysRtnCd();//系统返回码
			String sysRtnDesc = stream.getMsgBody().getSysRtnInf().getSysRtnDesc();//系统返回描述
			String bizStsCd = stream.getMsgBody().getBizInf().getBizStsCd();//业务返回码
			String bizStsDesc = stream.getMsgBody().getBizInf().getBizStsDesc();//业务返回描述
			orderInfo.setRetMsg(sysRtnCd+"-"+sysRtnDesc+" "+bizStsCd+"-"+bizStsDesc);
		}
		return orderInfo;
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if(StringUtils.isEmpty(request.getParameter("epccGwMsg"))){
			return false;
		}
		return true;
	}
}
